const reqlib = require('app-root-path').require
const btbAs = reqlib('/app/common/btb')

// asInfo的循环
function asInfoExtend (asInfo, v, index) {
  v.isKdjIncrease = btbAs.isKdjIncrease(asInfo, index)
  v.isKdjDecline = btbAs.isKdjDecline(asInfo, index)
  v.isMacdIncrease = btbAs.isMacdIncrease(asInfo, index)
  v.isMacdDecline = btbAs.isMacdDecline(asInfo, index)
  v.isEm5Increase = btbAs.isEmIncrease(asInfo, 'ema5', index)
  v.isEm5Decline = btbAs.isEmDecline(asInfo, 'ema5', index)
  v.isEm10Increase = btbAs.isEmIncrease(asInfo, 'ema10', index)
  v.isEm10Decline = btbAs.isEmDecline(asInfo, 'ema10', index)
  v.isEm20Increase = btbAs.isEmIncrease(asInfo, 'ema20', index)
  v.isEm20Decline = btbAs.isEmDecline(asInfo, 'ema20', index)
  v.isEmAllIncrease = v.isEm5Increase && v.isEm10Increase && v.isEm20Increase
  v.isEmAllDecline = v.isEm5Decline && v.isEm10Decline && v.isEm20Decline
  // 缓冲
  v.isBuffer = btbAs.isEmaInfoBuffer(v)
  // 盘整
  v.isReadjust = btbAs.isEmaInfoReadjust(v)
  v.isAllIncrease = btbAs.isEmaInfoAllIncrease(v)
  v.isAllDecline = btbAs.isEmaInfoAllDecline(v)
  v.isLongKong = btbAs.isEmaInfoLongKong(v)
  v.isLongDuo = btbAs.isEmaInfoLongDuo(v)
  v.isKong = btbAs.isEmaInfoKong(v)
  v.isDuo = btbAs.isEmaInfoDuo(v)
  // 夹着均线
  v.isCrimp5Line = btbAs.isCrimpLine(v, 'ema5')
  v.isCrimp10Line = btbAs.isCrimpLine(v, 'ema10')
  v.isCrimp20Line = btbAs.isCrimpLine(v, 'ema20')
}
exports.asInfoExtend = asInfoExtend

// btbList的循环
function btbListItemExtend (btbItem, asInfo, asList) {
  return {
    name: btbItem.name,
    code: btbItem.code,
    min_qty: btbItem.f_min_qty,
    max_qty: btbItem.f_max_qty,
    step_size: btbItem.f_step_size,
    min_notional: btbItem.f_min_notional,
    tick_size: btbItem.f_tick_size,
    proportion: btbItem.proportion,
    asInfo: [asInfo[0] || {}],
    emaMaxDiffAV: btbAs.maxDiffDays(asList, 2),
    macddd: btbAs.macdDownDays(asList),
    macdud: btbAs.macdUpDays(asList),
    kdjdd: btbAs.kdjDownDays(asList),
    kdjud: btbAs.kdjUpDays(asList),
    b510t20KeepDays: btbAs.b510t20KeepDays(asInfo),
    t510b20KeepDays: btbAs.t510b20KeepDays(asInfo),
    t20b10KeepDays: btbAs.t20b10KeepDays(asInfo),
    t10b20KeepDays: btbAs.t10b20KeepDays(asInfo),
    t20b5KeepDays: btbAs.t20b5KeepDays(asInfo),
    t5b20KeepDays: btbAs.t5b20KeepDays(asInfo),
    t10b5KeepDays: btbAs.t10b5KeepDays(asInfo),
    t5b10KeepDays: btbAs.t5b10KeepDays(asInfo),
    isKdjIncrease: btbAs.isKdjIncrease(asInfo),
    isKdjDecline: btbAs.isKdjDecline(asInfo),
    isMacdIncrease: btbAs.isMacdIncrease(asInfo),
    isMacdDecline: btbAs.isMacdDecline(asInfo),
    isEm5Increase: asInfo[0].isEm5Increase,
    isEm5Decline: asInfo[0].isEm5Decline,
    isEm10Increase: asInfo[0].isEm10Increase,
    isEm10Decline: asInfo[0].isEm10Decline,
    isEm20Increase: asInfo[0].isEm20Increase,
    isEm20Decline: asInfo[0].isEm20Decline,
    isEmAllIncrease: asInfo[0].isEmAllIncrease,
    isEmAllDecline: asInfo[0].isEmAllDecline,
    isBuffer: asInfo[0].isBuffer,
    isReadjust: asInfo[0].isReadjust,
    isAllIncrease: asInfo[0].isAllIncrease,
    isAllDecline: asInfo[0].isAllDecline,
    isLongKong: asInfo[0].isLongKong,
    isLongDuo: asInfo[0].isLongDuo,
    isKong: asInfo[0].isKong,
    isDuo: asInfo[0].isDuo,
    isCrimp5Line: asInfo[0].isCrimp5Line,
    isCrimp10Line: asInfo[0].isCrimp10Line,
    isCrimp20Line: asInfo[0].isCrimp20Line,
    ema5IncreaseDays: btbAs.emaIncreaseDeclineKeepDays(asInfo, 'isEm5Increase'),
    ema5DeclineDays: btbAs.emaIncreaseDeclineKeepDays(asInfo, 'isEm5Decline'),
    ema10IncreaseDays: btbAs.emaIncreaseDeclineKeepDays(asInfo, 'isEm10Increase'),
    ema10DeclineDays: btbAs.emaIncreaseDeclineKeepDays(asInfo, 'isEm10Decline'),
    ema20IncreaseDays: btbAs.emaIncreaseDeclineKeepDays(asInfo, 'isEm20Increase'),
    ema20DeclineDays: btbAs.emaIncreaseDeclineKeepDays(asInfo, 'isEm20Decline'),
    // 最近5天的涨幅
    days5NetChangeRatio: btbAs.getDaysNetChangeRatio(asInfo, 5),
    days10NetChangeRatio: btbAs.getDaysNetChangeRatio(asInfo, 10),
    days20NetChangeRatio: btbAs.getDaysNetChangeRatio(asInfo, 20),
    // 最近5天的涨幅 / 最近5天的实际波动之和
    days5RealWave: btbAs.getDaysRealWave(asInfo, 5),
    days10RealWave: btbAs.getDaysRealWave(asInfo, 10),
    days20RealWave: btbAs.getDaysRealWave(asInfo, 20),
    // 最近5天的涨幅 / 最近5天的最大波动之和
    days5MaxWave: btbAs.getDaysMaxWave(asInfo, 5),
    days10MaxWave: btbAs.getDaysMaxWave(asInfo, 10),
    days20MaxWave: btbAs.getDaysMaxWave(asInfo, 20),
    // 今天的涨幅 / 最近5天的实际波动之和
    nowRatio5: btbAs.getNowChangeRatio(asInfo, 5),
    days2RealMaxWave: btbAs.days2RealMaxWave(asInfo, 2),
    emaKeepDays: btbAs.emaKeepDays(asList)
  }
}
exports.btbListItemExtend = btbListItemExtend

// emInfoList的循环
function emInfoListItemExtend (em) {
  let kdjDiff = 0
  if (em.kdj) {
    kdjDiff = em.kdj.J - em.kdj.D
  }
  return {
    tr: em.tr || 0,
    ema5: em.ema5 || 0,
    ema10: em.ema10 || 0,
    ema20: em.ema20 || 0,
    kdj: em.kdj || {},
    macd: em.macd || 0,
    kdjDiff,
    volume: em.volume,
    open: em.open,
    high: em.high,
    low: em.low,
    close: em.close,
    netChangeRatio: em.netChangeRatio,
    realWave: em.realWave,
    maxNetChangeRatio: em.maxNetChangeRatio,
    diff510: em.diff510,
    diff520: em.diff520,
    diff1020: em.diff1020,
    sg: em.sg,
    isKdjIncrease: em.isKdjIncrease,
    isKdjDecline: em.isKdjDecline,
    isMacdIncrease: em.isMacdIncrease,
    isMacdDecline: em.isMacdDecline,
    isEm5Increase: em.isEm5Increase,
    isEm5Decline: em.isEm5Decline,
    isEm10Increase: em.isEm10Increase,
    isEm10Decline: em.isEm10Decline,
    isEm20Increase: em.isEm20Increase,
    isEm20Decline: em.isEm20Decline,
    isEmAllIncrease: em.isEmAllIncrease,
    isEmAllDecline: em.isEmAllDecline,
    isBuffer: em.isBuffer,
    isReadjust: em.isReadjust,
    isAllIncrease: em.isAllIncrease,
    isAllDecline: em.isAllDecline,
    isLongKong: em.isLongKong,
    isLongDuo: em.isLongDuo,
    isKong: em.isKong,
    isDuo: em.isDuo,
    isCrimp5Line: em.isCrimp5Line,
    isCrimp10Line: em.isCrimp10Line,
    isCrimp20Line: em.isCrimp20Line
  }
}
exports.emInfoListItemExtend = emInfoListItemExtend

// 更新指数信息的拓展
function indexUpdateItemExtend (v, em) {
  return {
    tr: em.tr || 0,
    ema5: em.ema5 || 0,
    ema10: em.ema10 || 0,
    ema20: em.ema20 || 0,
    kdj: em.kdj || {},
    macd: em.macd || 0,
    emaMaxDiffAV: v.emaMaxDiffAV,
    macddd: v.macddd || 0,
    macdud: v.macdud || 0,
    kdjdd: v.kdjdd || 0,
    kdjud: v.kdjud || 0,
    b510t20KeepDays: v.b510t20KeepDays || 0,
    t510b20KeepDays: v.t510b20KeepDays || 0,
    t20b10KeepDays: v.t20b10KeepDays || 0,
    t10b20KeepDays: v.t10b20KeepDays || 0,
    t20b5KeepDays: v.t20b5KeepDays || 0,
    t5b20KeepDays: v.t5b20KeepDays || 0,
    t10b5KeepDays: v.t10b5KeepDays || 0,
    t5b10KeepDays: v.t5b10KeepDays || 0,
    isKdjIncrease: v.isKdjIncrease || false,
    isKdjDecline: v.isKdjDecline || false,
    isMacdIncrease: v.isMacdIncrease || false,
    isMacdDecline: v.isMacdDecline || false,
    isEm5Increase: v.isEm5Increase || false,
    isEm5Decline: v.isEm5Decline || false,
    isEm10Increase: v.isEm10Increase || false,
    isEm10Decline: v.isEm10Decline || false,
    isEm20Increase: v.isEm20Increase || false,
    isEm20Decline: v.isEm20Decline || false,
    isEmAllIncrease: v.isEmAllIncrease || false,
    isEmAllDecline: v.isEmAllDecline || false,
    isBuffer: v.isBuffer || false,
    isReadjust: v.isReadjust || false,
    isAllIncrease: v.isAllIncrease || false,
    isAllDecline: v.isAllDecline || false,
    isLongKong: v.isLongKong || false,
    isLongDuo: v.isLongDuo || false,
    isKong: v.isKong || false,
    isDuo: v.isDuo || false,
    isCrimp5Line: v.isCrimp5Line || false,
    isCrimp10Line: v.isCrimp10Line || false,
    isCrimp20Line: v.isCrimp20Line || false,
    ema5IncreaseDays: v.ema5IncreaseDays || 0,
    ema5DeclineDays: v.ema5DeclineDays || 0,
    ema10IncreaseDays: v.ema10IncreaseDays || 0,
    ema10DeclineDays: v.ema10DeclineDays || 0,
    ema20IncreaseDays: v.ema20IncreaseDays || 0,
    ema20DeclineDays: v.ema20DeclineDays || 0,
    days5NetChangeRatio: v.days5NetChangeRatio || 0,
    days10NetChangeRatio: v.days10NetChangeRatio || 0,
    days20NetChangeRatio: v.days20NetChangeRatio || 0,
    days5RealWave: v.days5RealWave || 0,
    days10RealWave: v.days10RealWave || 0,
    days20RealWave: v.days20RealWave || 0,
    days5MaxWave: v.days5MaxWave || 0,
    days10MaxWave: v.days10MaxWave || 0,
    days20MaxWave: v.days20MaxWave || 0,
    days2RealMaxWave: v.days2RealMaxWave || 0,
    nowRatio5: v.nowRatio5 || 0,
    macdKeepDays: em.macd > 0 ? (v.macdud || 0) : (v.macddd || 0),
    emaKeepDays: v.emaKeepDays || 0,
    // 近的在右边
    emInfoList: v.emInfoList || [],
    open: em.open,
    high: em.high,
    low: em.low,
    close: em.close,
    netChangeRatio: em.netChangeRatio,
    // 实际涨幅 / 最大振幅
    realWave: em.realWave,
    //  最高和最低波动 high / low
    maxNetChangeRatio: em.maxNetChangeRatio,
    diff510: em.diff510,
    diff520: em.diff520,
    diff1020: em.diff1020,
    sg: em.sg,
    isDownLikeL: btbAs.isDownLikeL(v, em)
  }
}
exports.indexUpdateItemExtend = indexUpdateItemExtend
